package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjFault;
import com.zb.entity.ZjKnowledgeType;
import com.zb.entity.ZjKnowledgeTypeFault;
import com.zb.extend.KnowledgeTypeFaultExtend;
import com.zb.extend.KnowledgeTypeInstructionBookExtend;
import com.zb.mapper.ZjKnowledgeTypeFaultMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjFaultService;
import com.zb.service.ZjKnowledgeTypeFaultService;
import com.zb.entity.KnowledgeTypeRelationVo;
import com.zb.service.ZjKnowledgeTypeService;
import com.zb.vo.KnowledgeInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 知识库分类故障表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-13
 */
@Service
public class ZjKnowledgeTypeFaultServiceImpl extends ServiceImpl<ZjKnowledgeTypeFaultMapper, ZjKnowledgeTypeFault> implements ZjKnowledgeTypeFaultService {
    @Resource
    private ZjFaultService faultService;
    @Resource
    private ZjKnowledgeTypeService knowledgeTypeService;

    // 新增、修改、删除
    @Override
    public void save(Long custAccId, Long faultId, List<KnowledgeTypeRelationVo> knowledgeTypeVos, List<Long> delIds) {
        if (CollectionUtil.isNotEmpty(delIds)) {
            this.removeByIds(delIds);
        }
        if (CollectionUtil.isEmpty(knowledgeTypeVos)) {
            return;
        }
        List<ZjKnowledgeTypeFault> adds = new ArrayList<>();
        List<ZjKnowledgeTypeFault> updates = new ArrayList<>();
        ZjKnowledgeTypeFault typeFault = null;
        for (KnowledgeTypeRelationVo vo : knowledgeTypeVos) {
            typeFault = new ZjKnowledgeTypeFault();
            BeanUtil.copyProperties(vo, typeFault);
            if (null != typeFault.getId()) {
                updates.add(typeFault);
                continue;
            }
            typeFault.setId(IdWorker.getId());
            typeFault.setCustAccId(custAccId);
            typeFault.setFaultId(faultId);
            adds.add(typeFault);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 根据故障表ID获取知识库分类
    @Override
    public List<ZjKnowledgeTypeFault> getByFaultId(Long faultId) {
        QueryWrapper<ZjKnowledgeTypeFault> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id");
        wrapper.eq("fault_id", faultId);
        return this.list(wrapper);
    }

    // 根据故障ID删除
    @Override
    public void deleteBatchByFaultId(List<Long> faultIds) {
        if (CollectionUtil.isEmpty(faultIds)) {
            return;
        }
        QueryWrapper<ZjKnowledgeTypeFault> wrapper = new QueryWrapper<>();
        wrapper.in("fault_id", faultIds);
        this.remove(wrapper);
    }

    // 根据知识库分类ID获取故障信息
    @Override
    public List<KnowledgeTypeFaultExtend> getByKnowledgeTypeIds(List<Long> knowledgeTypeIds) {
        if (CollectionUtil.isEmpty(knowledgeTypeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeTypeFault> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id", "fault_id");
        wrapper.in("knowledge_type_id", knowledgeTypeIds);
        List<ZjKnowledgeTypeFault> typeFaultList = this.list(wrapper);
        if (CollectionUtil.isEmpty(typeFaultList)) {
            return new ArrayList<>();
        }

        List<Long> faultIds = typeFaultList.stream().map(ZjKnowledgeTypeFault::getFaultId).distinct().toList();
        QueryWrapper<ZjFault> faultQueryWrapper = new QueryWrapper<>();
        faultQueryWrapper.select("id", "name");
        faultQueryWrapper.in("id", faultIds);
        List<ZjFault> faultList = faultService.list(faultQueryWrapper);

        return typeFaultList.stream().map(type -> {
            KnowledgeTypeFaultExtend extend = new KnowledgeTypeFaultExtend();
            BeanUtil.copyProperties(type, extend);
            extend.setName(faultList.stream().filter(f -> Objects.equals(f.getId(), type.getFaultId())).map(ZjFault::getName).findFirst().orElse(""));
            return extend;
        }).toList();
    }

    // 根据知识仓库分类ID获取故障库
    @Override
    public List<KnowledgeInfoVo> getProductFault(Long knowledgeTypeId) {
        List<Long> knowledgeTypeIds = new ArrayList<>();
        knowledgeTypeIds.add(knowledgeTypeId);
        List<KnowledgeTypeFaultExtend> faultExtendList = getByKnowledgeTypeIds(knowledgeTypeIds);

        QueryWrapper<ZjKnowledgeType> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.select("name");
        typeQueryWrapper.eq("id", knowledgeTypeId);
        ZjKnowledgeType knowledgeType = knowledgeTypeService.getOne(typeQueryWrapper);

        return faultExtendList.stream().map(e -> {
            return new KnowledgeInfoVo(e.getFaultId(), e.getName(), "", knowledgeType.getName());
        }).toList();
    }

    // 获取故障库列表
    @Override
    public Map<String, Object> getUserMobileFaultList(Long knowledgeTypeId, PageUtil pageUtil) {
        PageUtil.setPageLimitMobile(pageUtil);
        QueryWrapper<ZjKnowledgeTypeFault> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id", "fault_id");
        wrapper.eq("knowledge_type_id", knowledgeTypeId);
        Page<ZjKnowledgeTypeFault> page = this.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), wrapper);
        if (CollectionUtil.isEmpty(page.getRecords())) {
            return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), new ArrayList<>());
        }

        List<Long> faultIds = page.getRecords().stream().map(ZjKnowledgeTypeFault::getFaultId).distinct().toList();
        QueryWrapper<ZjFault> faultQueryWrapper = new QueryWrapper<>();
        faultQueryWrapper.select("id", "name");
        faultQueryWrapper.in("id", faultIds).eq("is_disabled", false);
        List<ZjFault> faultList = faultService.list(faultQueryWrapper);

        List<KnowledgeInfoVo> voList = page.getRecords().stream().map(e -> {
            return new KnowledgeInfoVo(e.getFaultId(), faultList.stream().filter(f -> Objects.equals(f.getId(), e.getFaultId())).map(ZjFault::getName).findFirst().orElse(""), "", "");
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据故障库ID获取知识库信息
    @Override
    public List<KnowledgeTypeFaultExtend> getbyFaultIds(List<Long> faultIds) {
        if (CollectionUtil.isEmpty(faultIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeTypeFault> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id", "fault_id");
        wrapper.in("fault_id", faultIds);
        List<ZjKnowledgeTypeFault> knowledgeTypeFaultList = this.list(wrapper);
        if (CollectionUtil.isEmpty(knowledgeTypeFaultList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeType> knowledgeTypeQueryWrapper = new QueryWrapper<>();
        knowledgeTypeQueryWrapper.select("id", "name");
        knowledgeTypeQueryWrapper.in("id", knowledgeTypeFaultList.stream().map(ZjKnowledgeTypeFault::getKnowledgeTypeId).distinct().toList());
        List<ZjKnowledgeType> knowledgeTypeList = knowledgeTypeService.list(knowledgeTypeQueryWrapper);

        return knowledgeTypeFaultList.stream().map(type -> {
            KnowledgeTypeFaultExtend extend = new KnowledgeTypeFaultExtend();
            BeanUtil.copyProperties(type, extend);
            extend.setName(knowledgeTypeList.stream().filter(f -> Objects.equals(f.getId(), type.getKnowledgeTypeId())).map(ZjKnowledgeType::getName).findFirst().orElse(""));
            return extend;
        }).toList();
    }
}
